//
//  TravelDetailViewController.swift
//  AirTravel
//
//  Created by apple on 2021/11/24.
//

import UIKit
import HandyJSON
class TravelDetailViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
        switch section {
        case 1:
            return 30
        default:
            return 0
        }
    }
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?{
        let footView = UIView.init()
        footView.backgroundColor = UIColor.clear
        return footView
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.section {
        case 0:
            return 250
        default:
            return 340
        }
    }
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
        let headView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HotelHeaderView") as! HotelHeaderView
        switch section {
        case 1:
            headView.titleLab.text = "景票门票"
            headView.img.isHidden = true
            headView.leftLine.constant = 18
//        case 2:
//            headView.titleLab.text = "网友评价"
//            headView.img.isHidden = true
//            headView.leftLine.constant = 18
        default:
            print("1")
        }
        
        return headView
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1:
            return 1
        default:
            return 10
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "TravelDetailHeaderTableViewCell", for: indexPath) as! TravelDetailHeaderTableViewCell
            cell.title.text = dataModel?.name
            cell.time.text = dataModel?.open_time_desc
            cell.adress.text = dataDetailModel?.Address
            cell.tsLabel.text = dataDetailModel?.OpenTimeDesc
            return cell
            
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "TravelDetailCenterTableViewCell", for: indexPath) as! TravelDetailCenterTableViewCell
            cell.model = self.dataDetailModel
            cell.setUI()
            return cell
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: "TravelDetailHeaderTableViewCell", for: indexPath) as! TravelDetailHeaderTableViewCell
 
            return cell
        }
        
        
        
        
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tab.deselectRow(at: indexPath, animated: true)
    }
    
    var dataModel:CitySpotListModel?
    
    var dataDetailModel:TravelDetailModel?

    @IBOutlet weak var img: UIImageView!
    @IBOutlet weak var tab: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.tab.register(UINib.init(nibName: "HotelHeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: "HotelHeaderView")
        
        
        img.kf.setImage(with: URL.init(string: dataModel?.image_url ?? ""))
        getData()
        // Do any additional setup after loading the view.
    }
    
    
    func getData(){
//        let dic = ["scenicSpotId" : dataModel?.id ?? 0 ]
        
        let dic = ["scenicSpotId" : "229" ]
        
        NetWorkRequest(API1.getScenicSpotDetail(parameters: dic)) { responds in
            guard let model = JSONDeserializer<TravelDetailMianModel>.deserializeFrom(json: "\(responds)") else {
                return
                
            }
            self.dataDetailModel = model.data?.first
            self.tab.reloadData()
            
            
        }
    }
    

    @IBAction func backClick(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
}
